const { contextBridge, ipcRenderer } = require('electron');

// 暴露安全的 API 给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 文件操作
  saveFileDialog: (defaultPath) => ipcRenderer.invoke('save-file-dialog', defaultPath),
  saveFile: (filePath, content) => ipcRenderer.invoke('save-file', filePath, content),
  exportFileDialog: (defaultPath, format) => ipcRenderer.invoke('export-file-dialog', defaultPath, format),

  // 菜单事件监听
  onMenuNewFile: (callback) => ipcRenderer.on('menu-new-file', callback),
  onMenuOpenFile: (callback) => ipcRenderer.on('menu-open-file', callback),
  onMenuSaveFile: (callback) => ipcRenderer.on('menu-save-file', callback),
  onMenuSaveAs: (callback) => ipcRenderer.on('menu-save-as', callback),
  onMenuExportHTML: (callback) => ipcRenderer.on('menu-export-html', callback),
  onMenuExportPDF: (callback) => ipcRenderer.on('menu-export-pdf', callback),
  onMenuViewMode: (callback) => ipcRenderer.on('menu-view-mode', callback),
  onMenuToggleTheme: (callback) => ipcRenderer.on('menu-toggle-theme', callback),

  // 清理事件监听器
  removeAllListeners: (channel) => ipcRenderer.removeAllListeners(channel),

  // 检查是否在 Electron 环境中
  isElectron: true
});
